package com.example.tiku7;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ClipDrawable;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.example.tiku7.fragment.AFragment;
import com.example.tiku7.fragment.BlankFragment;
import com.example.tiku7.fragment.ClankFragment2;
import com.example.tiku7.fragment.DlankFragment;
import com.example.tiku7.net.View_Mian;
import com.google.android.material.navigation.NavigationView;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private Toolbar mToob;
    private TextView mSeek;
    private TextView mShare;
    private RecyclerView mRecy;
    private TabLayout mTab1;
    private ArrayList<Bean.ResultsBean> beanArrayList;
    private NavigationView mNava;
    private DrawerLayout mDr;
    private ViewPager mVp;
    private TabLayout mTab;
    private LinearLayout mLin;

    private ArrayList<Fragment> fragments;
    private FragmentManager manager;
    private Vp_Fragment vp_fragment;
    private PopupWindow popupWindow;
    private Recy_Adapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        mToob = (Toolbar) findViewById(R.id.toob);
        mNava = (NavigationView) findViewById(R.id.nava);
        mDr = (DrawerLayout) findViewById(R.id.dr);
        mVp = (ViewPager) findViewById(R.id.vp);
        mTab = (TabLayout) findViewById(R.id.tab);
        mLin = (LinearLayout) findViewById(R.id.lin);
        mToob.setTitle("");
        setSupportActionBar(mToob);

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, mDr, mToob, R.string.app_name, R.string.app_name);
        mDr.addDrawerListener(toggle);
        toggle.syncState();
         mNava.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
             @Override
             public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                 switch (menuItem.getItemId()){
                     case R.id.title2:
                         Intent intent = new Intent(MainActivity.this, MainActivity2.class);
                         startActivity(intent);
                         break;
                 }
                 return false;
             }
         });
        mDr.addDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {
                int right = mNava.getRight();
                mLin.setX(right);
            }

            @Override
            public void onDrawerOpened(@NonNull View drawerView) {

            }

            @Override
            public void onDrawerClosed(@NonNull View drawerView) {

            }

            @Override
            public void onDrawerStateChanged(int newState) {

            }
        });

        manager = getSupportFragmentManager();
        fragments = new ArrayList<>();
        fragments.add(new AFragment());
        fragments.add(new BlankFragment());
        fragments.add(new ClankFragment2());
        fragments.add(new DlankFragment());
        vp_fragment = new Vp_Fragment(manager, fragments);

        mVp.setAdapter(vp_fragment);
        mTab.setupWithViewPager(mVp);

        mTab.getTabAt(0).setText("首页").setIcon(R.drawable.rej);
        mTab.getTabAt(1).setText("我的").setIcon(R.drawable.resd);
        mTab.getTabAt(2).setText("联系人").setIcon(R.drawable.resg);
        mTab.getTabAt(3).setText("收藏").setIcon(R.drawable.resk);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.xuanze,menu);

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.sousuo:
                //写popupWindow 首先写一个布局
                //new 一个popupWindow （布局的控件，宽和高）
                //popupWindow的颜色
                //设置setOutsideTouchable 点击边界取消popupWindow
                //设置位置
                View view = LayoutInflater.from(this).inflate(R.layout.activity_pp, null);
                popupWindow = new PopupWindow(view, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                popupWindow.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.colorAccent)));
                popupWindow.setOutsideTouchable(true);
                popupWindow.showAtLocation(view, Gravity.BOTTOM,0,0);
                break;
            case R.id.fenxiang:

                break;

        }

        return super.onOptionsItemSelected(item);
    }


}
